package kr.hkit.powerjava.chapter26.Young;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class EmployeeDAO implements DaoInterface<Employee>{

	private Connection connection ;
	
	public EmployeeDAO(Connection connection) {
		this.connection = connection;
	}

	@Override
	public void insertItem(Employee item) throws SQLException {
		String sql = "insert into employee values(?,?,?,?,?,?)";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		pstmt.setString(3, item.getTitle());
		pstmt.setInt(4, item.getManager());
		pstmt.setInt(5, item.getSalary());
		pstmt.setInt(6, item.getDno());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override
	public void deleteItem(Employee item) throws SQLException {
		String sql = "delete from employee where empno=?, empname=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1,item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
		
	}

	@Override
	public void updateItem(Employee item) throws SQLException {
		String sql = "update employee set title=?, manager=?, salary=?, dno=? "
				+ "where empno=?, empname=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getTitle());
		pstmt.setInt(2, item.getManager());
		pstmt.setInt(3, item.getSalary());
		pstmt.setInt(4, item.getDno());
		pstmt.setInt(5, item.getEmpNo());
		pstmt.setString(6, item.getEmpName());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override
	public Employee searchItem(Employee item) throws SQLException {
		Employee employee = null;
		String sql = "select empno, empname, title, manager, salary, dno "
				+ "from employee where empno=?, empname=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		ResultSet rs = pstmt.executeQuery();
		
		if (rs.next()) {
			employee = new Employee();
			
		}
		return null;
	}

	@Override
	public ArrayList<Employee> listItems() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
